package com.kylin.common.taglib;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.jsp.JspException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import com.kylin.common.Constants;
import com.kylin.common.entity.SelectOption;

public class TestTag extends SelectTag {
	protected String dictName;

	public String getDictName() {
		return dictName;
	}

	public void setDictName(String dictName) {
		this.dictName = dictName;
	}

	public int doStartTag() throws JspException {
		int rs = 2;//super.doStartTag();
        ResponseUtils.write(pageContext, renderSelectStartElement());
        pageContext.setAttribute("org.apache.struts.taglib.html.SELECT", this);
        calculateMatchValues();

		try {
			pageContext.getOut().println();
			pageContext.getOut().println(getOptions());
			pageContext.getOut().println();
		} catch (IOException e) {
			throw new JspException(e);
		}
		return rs;
	}

    private void calculateMatchValues()
    throws JspException
{
    if(value != null)
    {
 System.out.println("\n t1 value="+value);
    	match = new String[1];
        match[0] = value;
    } else
    {
    	 System.out.println("\n t2 value=null,name="+name);
        Object bean = RequestUtils.lookup(pageContext, name, null);
   	 System.out.println("\n t2 Object bean ="+bean.toString()+",bn="+bean.getClass().getName());
       if(bean == null)
        {
            JspException e = new JspException(messages.getMessage("getter.bean", name));
            RequestUtils.saveException(pageContext, e);
            throw e;
        }
        try
        {
        	 System.out.println("\n t2 property="+property);
            match = BeanUtils.getArrayProperty(bean, property);
            if(match == null)
                match = new String[0];
        }
        catch(IllegalAccessException e)
        {
            RequestUtils.saveException(pageContext, e);
            throw new JspException(messages.getMessage("getter.access", property, name));
        }
        catch(InvocationTargetException e)
        {
            Throwable t = e.getTargetException();
            RequestUtils.saveException(pageContext, t);
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        }
        catch(NoSuchMethodException e)
        {
            RequestUtils.saveException(pageContext, e);
            throw new JspException(messages.getMessage("getter.method", property, name));
        }
    }
}

    private String getOptions() {
		StringBuffer sb = new StringBuffer("<option/>");
		if (this.dictName == null
				|| !(Constants.dictionarys.get(dictName) instanceof SelectOption[]) ) {
			return "<option/>";
		}

		SelectOption[] options = (SelectOption[]) Constants.dictionarys.get(dictName);
		for (int i = 0; i < options.length; i++) {
			sb.append("<option value=");
			sb.append("\"").append(options[i].getValue()).append("\"");
			if(this.matchValue(options[i].getValue()))
				sb.append(" selected=\"true\"");
			sb.append(" >");
			sb.append(options[i].getText());
			sb.append("</option>\n");
		}
		return sb.toString();
	}

    private boolean matchValue(String v){
    	if(this.match == null || this.match.length ==0)
    		return false;
    	for(int i =0;i<this.match.length;i++){
    		if(match[i]!= null)
    			if(match[i].equals(v))
    				return true;
    	}
    	return false;
    }
}
